package com.srwl.mytx.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.srwl.mytx.Constant;
import com.srwl.mytx.R;

public class TaskProgressView extends LinearLayout {
    private static final String TAG = "TaskProgressView";
    private TextView tv_content1, tv_content2, tv_content3, tv_content4, tv_content5;
    private RelativeLayout rl_circle1, rl_circle2, rl_circle3, rl_circle4, rl_circle5;
    private ImageView iv_1, iv_2, iv_3, iv_4, iv_5;
    private View line1, line2, line3, line4;
    private ProgressBar progressBar2, progressBar3, progressBar4;

    public TaskProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TaskProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public TaskProgressView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.task_progress_view, this);
        tv_content1 = findViewById(R.id.tv_content_1);
        tv_content2 = findViewById(R.id.tv_content_2);
        tv_content3 = findViewById(R.id.tv_content_3);
        tv_content4 = findViewById(R.id.tv_content_4);
        tv_content5 = findViewById(R.id.tv_content_5);
        rl_circle1 = findViewById(R.id.rl_circle_1);
        rl_circle2 = findViewById(R.id.rl_circle_2);
        rl_circle3 = findViewById(R.id.rl_circle_3);
        rl_circle4 = findViewById(R.id.rl_circle_4);
        rl_circle5 = findViewById(R.id.rl_circle_5);
        iv_1 = findViewById(R.id.iv_1);
        iv_2 = findViewById(R.id.iv_2);
        iv_3 = findViewById(R.id.iv_3);
        iv_4 = findViewById(R.id.iv_4);
        iv_5 = findViewById(R.id.iv_5);
        line1 = findViewById(R.id.line_1);
        line2 = findViewById(R.id.line_2);
        line3 = findViewById(R.id.line_3);
        line4 = findViewById(R.id.line_4);
        progressBar2 = findViewById(R.id.progress_bar_2);
        progressBar3 = findViewById(R.id.progress_bar_3);
        progressBar4 = findViewById(R.id.progress_bar_4);
    }


    public void setTaskStatus(int status, boolean isEmployer) {

        switch (status) {
            case Constant.TASK_STATUS_INIT:
                stage2Executing();
                stage3Waite();
                stage4Waite();
                stage5Waite();
                break;
            case Constant.TASK_STATUS_CONFIRM:
            case Constant.TASK_STATUS_EMPLOYER_REFUSE_FINISH_APPLY:
                stage2Complete();
                stage3Executing();
                stage4Waite();
                stage5Waite();
                break;

            case Constant.TASK_STATUS_EMPLOYEE_REFUSE_CANCEL: //雇主取消任务，被雇佣者拒绝取消该任务，雇主方隐藏掉进度View,被雇佣者 看到任务回归正常
                if (isEmployer) {
                    setVisibility(GONE);
                } else {
                    setVisibility(VISIBLE);
                    stage2Complete();
                    stage3Executing();
                    stage4Waite();
                    stage5Waite();
                }

                break;
            case Constant.TASK_STATUS_APPLY_FINISH_TASK:
                stage2Complete();
                stage3Complete();
                stage4Executing();
                stage5Waite();
                break;
            case Constant.TASK_STATUS_CHECK_SUCCESS:
                stage2Complete();
                stage3Complete();
                stage4Complete();
                stage5Complete();
                break;

            default:
                Log.i(TAG, "setTaskStatus:  该状态没有做处理：" + status);
                setVisibility(GONE);
        }
    }

    private void stage2Waite() {
        rl_circle2.setBackground(getResources().getDrawable(R.drawable.gray_ring));
        iv_2.setImageResource(R.drawable.tx_correct_gray);
        line2.setBackgroundColor(getResources().getColor(R.color.gray_pressed));
        iv_2.setVisibility(VISIBLE);
        progressBar2.setVisibility(GONE);
    }

    private void stage3Waite() {
        rl_circle3.setBackground(getResources().getDrawable(R.drawable.gray_ring));
        iv_3.setImageResource(R.drawable.tx_correct_gray);
        line3.setBackgroundColor(getResources().getColor(R.color.gray_pressed));
        iv_3.setVisibility(VISIBLE);
        progressBar3.setVisibility(GONE);
    }

    private void stage4Waite() {
        rl_circle4.setBackground(getResources().getDrawable(R.drawable.gray_ring));
        iv_4.setImageResource(R.drawable.tx_correct_gray);
        line4.setBackgroundColor(getResources().getColor(R.color.gray_pressed));
        iv_4.setVisibility(VISIBLE);
        progressBar4.setVisibility(GONE);
    }

    private void stage5Waite() {
        rl_circle5.setBackground(getResources().getDrawable(R.drawable.gray_ring));
        iv_5.setImageResource(R.drawable.tx_correct_gray);
    }

    private void stage2Executing() {
        rl_circle2.setBackground(getResources().getDrawable(R.drawable.orange_ring));
        line2.setBackground(getResources().getDrawable(R.drawable.orange_to_gray_gradient));
        iv_2.setVisibility(INVISIBLE);
        progressBar2.setVisibility(VISIBLE);

    }

    private void stage3Executing() {
        rl_circle3.setBackground(getResources().getDrawable(R.drawable.orange_ring));
        line3.setBackground(getResources().getDrawable(R.drawable.orange_to_gray_gradient));
        iv_3.setVisibility(INVISIBLE);
        progressBar3.setVisibility(VISIBLE);
    }

    private void stage4Executing() {
        rl_circle4.setBackground(getResources().getDrawable(R.drawable.orange_ring));
        line4.setBackground(getResources().getDrawable(R.drawable.orange_to_gray_gradient));
        iv_4.setVisibility(INVISIBLE);
        progressBar4.setVisibility(VISIBLE);
    }


    private void stage2Complete() {
        rl_circle2.setBackground(getResources().getDrawable(R.drawable.orange_circle));
        iv_2.setImageResource(R.drawable.tx_correct_white);
        line2.setBackgroundColor(getResources().getColor(R.color.orange));
        iv_2.setVisibility(VISIBLE);
        progressBar2.setVisibility(INVISIBLE);
    }

    private void stage3Complete() {
        rl_circle3.setBackground(getResources().getDrawable(R.drawable.orange_circle));
        iv_3.setImageResource(R.drawable.tx_correct_white);
        line3.setBackgroundColor(getResources().getColor(R.color.orange));
        iv_3.setVisibility(VISIBLE);
        progressBar3.setVisibility(INVISIBLE);
    }

    private void stage4Complete() {
        rl_circle4.setBackground(getResources().getDrawable(R.drawable.orange_circle));
        iv_4.setImageResource(R.drawable.tx_correct_white);
        line4.setBackgroundColor(getResources().getColor(R.color.orange));
        iv_4.setVisibility(VISIBLE);
        progressBar4.setVisibility(INVISIBLE);
    }

    private void stage5Complete() {
        rl_circle5.setBackground(getResources().getDrawable(R.drawable.orange_circle));
        iv_5.setImageResource(R.drawable.tx_correct_white);
    }


}
